package com.fc.controller;


import com.fc.anno.Log;
import com.fc.context.BaseContext;
import com.fc.dto.ExpenseDTO;
import com.fc.dto.pageQuery.ExpensePageQueryDTO;
import com.fc.entity.Expense;
import com.fc.result.PageResult;
import com.fc.result.Result;
import com.fc.service.IExpenseService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.web.bind.annotation.*;


@Api(tags = "支出")
@RestController
@RequestMapping("/expense")
public class ExpenseController {

    @Autowired
    private IExpenseService expenseService;


    /**
     * 新增司机支出
     *
     * @param expenseDTO
     * @return
     */

    @ApiOperation("新增支出记录")
    @PostMapping
    public Result save(@RequestBody ExpenseDTO expenseDTO) {
        expenseService.saveExpense(expenseDTO);
        return Result.success();
    }

    /**
     * 修改司机当前物流过程中的支出
     *
     * @param expenseDTO
     * @return
     */
    @ApiOperation("修改支出记录")
    @PutMapping
    public Result update(@RequestBody ExpenseDTO expenseDTO) {
        expenseService.updateExpense(expenseDTO);
        return Result.success();
    }


    /**
     * 审核通过
     *
     * @param id
     * @return
     */
    @Log
    @PreAuthorize("hasRole('ADMIN')")
    @ApiOperation("审核通过")
    @PutMapping("/approve/{id}")
    public Result approve(@PathVariable Long id) {
        expenseService.approve(id);
        return Result.success();
    }

    /**
     * 审核拒绝
     *
     * @param id
     * @return
     */
    @Log
    @PreAuthorize("hasRole('ADMIN')")
    @ApiOperation("审核拒绝")
    @PutMapping("/reject/{id}")
    public Result reject(@PathVariable Long id) {
        expenseService.reject(id);
        return Result.success();
    }

    /**
     * 删除司机当前物流过程中的支出
     *
     * @param id
     * @return
     */
    @ApiOperation("删除支出记录")
    @DeleteMapping("/{id}")
    public Result delete(@PathVariable Long id) {
        expenseService.removeById(id);
        return Result.success();
    }


    /**
     * 查看支出详情
     *
     * @param id
     * @return
     */
    @ApiOperation("查看支出详情")
    @GetMapping("/{id}")
    public Result<Expense> detail(@PathVariable Long id) {
        Expense expense = expenseService.getById(id);
        return Result.success(expense);
    }


    /**
     * 管理员查看支出记录列表
     *
     * @param expensePageQueryDTO
     * @return
     */
    @PreAuthorize("hasRole('ADMIN')")
    @ApiOperation("管理员查看支出记录列表")
    @PostMapping("/page")
    public Result<PageResult> page(@RequestBody ExpensePageQueryDTO expensePageQueryDTO) {
        PageResult pageResult = expenseService.listExpense(expensePageQueryDTO);
        return Result.success(pageResult);
    }

    /**
     * 司机查询支出记录列表
     *
     * @param expensePageQueryDTO
     * @return
     */
    @ApiOperation("司机查询支出记录列表")
    @PostMapping("/list")
    public Result<PageResult> list(@RequestBody ExpensePageQueryDTO expensePageQueryDTO) {
        expensePageQueryDTO.setDriverId(BaseContext.getCurrentId());
        PageResult pageResult = expenseService.listExpense(expensePageQueryDTO);
        return Result.success(pageResult);
    }
}
